package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TbSearchHistory;
import com.ruoyi.system.service.ITbSearchHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 搜索记录Controller
 * 
 * @author ruoyi
 * @date 2024-04-07
 */
@RestController
@RequestMapping("/system/history")
public class TbSearchHistoryController extends BaseController
{
    @Autowired
    private ITbSearchHistoryService tbSearchHistoryService;

    /**
     * 查询搜索记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:history:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbSearchHistory tbSearchHistory)
    {
        startPage();
        List<TbSearchHistory> list = tbSearchHistoryService.selectTbSearchHistoryList(tbSearchHistory);
        return getDataTable(list);
    }

    /**
     * 导出搜索记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:history:export')")
    @Log(title = "搜索记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbSearchHistory tbSearchHistory)
    {
        List<TbSearchHistory> list = tbSearchHistoryService.selectTbSearchHistoryList(tbSearchHistory);
        ExcelUtil<TbSearchHistory> util = new ExcelUtil<TbSearchHistory>(TbSearchHistory.class);
        util.exportExcel(response, list, "搜索记录数据");
    }

    /**
     * 获取搜索记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:history:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbSearchHistoryService.selectTbSearchHistoryById(id));
    }

    /**
     * 新增搜索记录
     */
    @PreAuthorize("@ss.hasPermi('system:history:add')")
    @Log(title = "搜索记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbSearchHistory tbSearchHistory)
    {
        return toAjax(tbSearchHistoryService.insertTbSearchHistory(tbSearchHistory));
    }

    /**
     * 修改搜索记录
     */
    @PreAuthorize("@ss.hasPermi('system:history:edit')")
    @Log(title = "搜索记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbSearchHistory tbSearchHistory)
    {
        return toAjax(tbSearchHistoryService.updateTbSearchHistory(tbSearchHistory));
    }

    /**
     * 删除搜索记录
     */
    @PreAuthorize("@ss.hasPermi('system:history:remove')")
    @Log(title = "搜索记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbSearchHistoryService.deleteTbSearchHistoryByIds(ids));
    }
}
